/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.dal.client;

import ezcms.dto.FileResource;
import ezcms.dto.Role;
import ezcms.dto.User;
import ezcms.exceptions.DataLayerException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class TestClient {

    public static void main(String[] args) {
//        Logger logger = LoggerFactory.getLogger(TestClient.class);
//        logger.info("Hello log");
//        if (true) {
//            return;
//        }

        DAOUtil dao = new DAOUtil();
        try {
            List<Role> roles = dao.getRoleDAO().getAllRoles();

            Role r = dao.getRoleDAO().getRoleByName("Admin");
            if (r == null) {
                r = new Role("Admin", "Admin role");
                dao.getRoleDAO().saveRole(r);
                r = dao.getRoleDAO().getRoleByName("Admin");
            }

            User u = dao.getUserDAO().getUser("admin1");
            if (u == null) {
                u = new User("admin1", "p@ssw0rd");
                u.setIntroduction("Default admin account.");
                dao.getUserDAO().saveUser(u);
                u = dao.getUserDAO().getUser("admin1");
            }
            if (u.getRoles() == null) {
                u.setRoles(new ArrayList<Role>());
            } else {
                System.out.println("Roles list is existing");
            }
            if (u.getRoles().isEmpty()) {
                u.getRoles().add(r);
                System.out.println("Added a role ...");
            }
            if (u.getAvatar() == null) {
                FileResource avatar = new FileResource(u, "avatar.jpg", "Admin1's avatar", "file1.jpg");
                dao.getResourceDAO().saveResource(avatar);
                u.setAvatar(avatar);
                dao.getUserDAO().saveUser(u);
            }
            u = dao.getUserDAO().getUser("admin1");

            for (Role role : roles) {
                System.out.println(role);
            }
            if (u != null) {
                System.out.println(u);
            } else {
                System.out.println("Can't find u");
            }

        } catch (DataLayerException ex) {
            //TODO: log
        }
    }
}
